﻿/*****************************************************************************  

 Version: 0.1
 Website: http://www.visuagile.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VisualAgile.Model;
using System.Collections;

namespace VisualAgile.DTO.Mappers
{
    public class ReleaseDTOMapper:BaseDTOMapper
    {
        public static ReleaseDTO MapToDTO(Release model)
        {
            ReleaseDTO dto = new ReleaseDTO();
            IList<FeatureDTO> featureDTO = new List<FeatureDTO>();
            IList<IterationDTO> iterationDTO = new List<IterationDTO>();
            
            dto.ID = model.ReleaseID;
            dto.ProjectID = model.ProjectID;
            dto.Name = model.Name;
            dto.PeriodFrom = model.PeriodFrom;
            dto.PeriodTo = model.PeriodTo;
            dto.CreateTime = model.CreateTime;
            dto.Creator = model.Creator;

            //load features
            foreach (Feature feature in model.Features)
            {
                featureDTO.Add(FeatureDTOMapper.MapToDTO(feature));
            }
            dto.items = featureDTO;

            //load iteration
            foreach (Iteration iteration in model.Iterations)
            {
                iterationDTO.Add(IterationDTOMapper.MapToDTO(iteration));
            }
            dto.tabItems = iterationDTO;
           
            return dto;
        }

        public static Release MapFromDTO(ReleaseDTO dto)
        {
            Release release = new Release();            
            release.ReleaseID = dto.ID;
            release.ProjectID = dto.ProjectID;            
            release.Name = dto.Name;
            release.PeriodFrom = dto.PeriodFrom.ToLocalTime();
            release.PeriodTo = dto.PeriodTo.ToLocalTime();
            release.CreateTime = dto.CreateTime;
            release.Creator = dto.Creator;

            //load features
            if (dto.items != null)
            {
                foreach (FeatureDTO featureDTO in dto.items)
                {
                    release.AddFeature(FeatureDTOMapper.MapFromDTO(featureDTO));
                }
            }

            ////load iterations
            //if (dto.tabItems != null)
            //{
            //    foreach (IterationDTO iterationDTO in dto.tabItems)
            //    {
            //        release.AddIteration(IterationDTOMapper.MapFromDTO(iterationDTO));
            //    }
            //}

            IList iterations = ManagerFactory.IterationManager.GetByRelease(release.ReleaseID);

            foreach (Iteration iteration in iterations)
            {
                release.AddIteration(iteration);
            }
          
            return release;
        }
    }
}
 